package com.xie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xie.domain.Result;
import com.xie.pojo.Potion;
import com.xie.pojo.Salary;
import com.xie.service.SalaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "工资管理")
@RestController
@RequestMapping("/Salarys")
public class SalaryController {
    @Autowired
    private SalaryService salaryService;

    /**
     * 分页查询
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/{pageIndex}/{pageSize}")
    @ApiOperation("分页查询工资信息")
    public Result getPage(@PathVariable Integer pageIndex,
                          @PathVariable  Integer pageSize){
        Page page=new Page(pageIndex,pageSize);
        IPage<Salary> page1 = salaryService.page(page);
        if (page1 != null) {
            return Result.ok(page1);
        }
        return Result.fail();
    }

    /**
     * 模糊查询工资
     * @param params
     * @return
     */
    @GetMapping("find/{params}")
    @ApiOperation("模糊查询工资信息")
    public Result FindPage(@PathVariable String params){
        LambdaQueryWrapper<Salary> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Salary::getSalaryId,params)
                .or().like(Salary::getSalaryMoney,params)
                .or().like(Salary::getSalaryName,params)
                .or().like(Salary::getSalaryNo,params)
        ;
        List<Salary> list = salaryService.list(lambdaQueryWrapper);
        if (list != null) return Result.ok(list);
        return Result.fail();
    }

    /**
     * 添加工资信息
     * @param salary
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("添加员工工资信息")
    public Result addSalary(@RequestBody Salary salary) {
        boolean save = salaryService.save(salary);
        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }


    /**
     * 修改工资信息
     * @param salary
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改员工工资信息")
    public Result updateSalary(@RequestBody Salary salary) {
        boolean update = salaryService.updateById(salary);
        if (update) {
            return Result.ok();
        }
        return Result.fail();
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("根据id查询工资信息")
    public Result findSalaryById(@PathVariable Long id) {
        Salary salary =salaryService.getById(id);
        if (salary != null) {
            return Result.ok(salary);
        }
        return Result.fail();
    }


    /**
     * 查询全部
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询所有工资信息")
    public Result exportSalarylist(){
        List<Salary> list = salaryService.list();
        if (list!= null) {
            return Result.ok(list);
        }
        return Result.fail();
    }

    /**
     * 根据id删除工资
     * @param id
     * @return
     */
    @PostMapping("/removeById/{id}")
    @ApiOperation("根据id删除工资信息")
    public Result deletePotion(@PathVariable Long id) {
        boolean save = salaryService.removeById(id);
        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }
}
